package com.whfc.ms.api.fmam.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.fmam.entity.dto.FmamWeighRoomDTO;
import com.whfc.fmam.entity.dto.FmamWeighRoomFvsDeviceDTO;
import com.whfc.fmam.entity.dto.LicenseFileDTO;
import com.whfc.fmam.entity.qo.WeighRoomAddQO;
import com.whfc.fmam.entity.qo.WeighRoomEditQO;
import com.whfc.fmam.entity.qo.WeighRoomFvsDeviceAddQO;
import com.whfc.fmam.service.FmamLicenseService;
import com.whfc.fmam.service.FmamWeighRoomService;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.dto.FvsRecordDTO;
import com.whfc.fvs.dto.FvsStreamDTO;
import com.whfc.fvs.enums.FvsDeviceType;
import com.whfc.fvs.param.FvsDeviceListParam;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.ms.api.fmam.service.WeighRoomService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author sun_guodong
 * @description
 * @date 2019-11-13
 */
@Service
public class WeighRoomServiceImpl implements WeighRoomService {
    @DubboReference(interfaceClass = FmamWeighRoomService.class, version = "1.0.0")
    private FmamWeighRoomService fmamWeighRoomService;

    @DubboReference(interfaceClass = FmamLicenseService.class, version = "1.0.0")
    private FmamLicenseService fmamLicenseService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    /**
     * 新增磅房
     *
     * @param request
     */
    @Override
    public void addWeighRoom(WeighRoomAddQO request) {
        fmamWeighRoomService.addWeighRoom(request);
    }

    /**
     * 删除磅房
     *
     * @param id
     */
    @Override
    public void delWeighRoom(Integer id) {
        fmamWeighRoomService.delWeighRoom(id);
    }

    /**
     * 编辑磅房
     *
     * @param request
     */
    @Override
    public void editWeighRoom(WeighRoomEditQO request) {
        fmamWeighRoomService.editWeighRoom(request);
    }

    /**
     * 查看磅房的授权文件
     *
     * @param weighRoomId
     * @return
     */
    @Override
    public ListData<LicenseFileDTO> licenseFileList(Integer weighRoomId) {
        return fmamWeighRoomService.licenseFileList(weighRoomId);
    }

    /**
     * 生成授权文件
     *
     * @param id
     */
    @Override
    public void licenseGenerate(Integer id) {
        fmamLicenseService.licenseGenerate(id);
    }

    /**
     * 磅房列表页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param deptId
     * @return
     */
    @Override
    public PageData<FmamWeighRoomDTO> listFmamWeighRoomDTO(SysUser user, Integer pageNum, Integer pageSize, Integer deptId) {
        PageData<FmamWeighRoomDTO> pageData = fmamWeighRoomService.listFmamWeighRoomDTO(pageNum, pageSize, deptId);
        return pageData;
    }

    @Override
    public ListData<FmamWeighRoomDTO> listFmamWeighRoomDTO(Integer deptId) {
        ListData<FmamWeighRoomDTO> listData = fmamWeighRoomService.listFmamWeighRoomDTO(deptId);
        return listData;
    }

    /**
     * 磅房视频视频列表
     *
     * @param weighRoomId
     * @return
     */
    @Override
    public List<FmamWeighRoomFvsDeviceDTO> videoList(Integer weighRoomId) {
        //查找项目已经添加的视频列表
        List<FmamWeighRoomFvsDeviceDTO> weighRoomFvsDevics = fmamWeighRoomService.fvsDeviceList(weighRoomId);
        if (weighRoomFvsDevics == null || weighRoomFvsDevics.isEmpty()) {
            return new ArrayList<>();
        }
        //取出设备ID 查找监控设备信息
        Map<Integer, FmamWeighRoomFvsDeviceDTO> map = new HashMap<>();
        for (FmamWeighRoomFvsDeviceDTO fseLiftFvsDeviceDTO : weighRoomFvsDevics) {
            map.put(fseLiftFvsDeviceDTO.getFvsDeviceId(), fseLiftFvsDeviceDTO);
        }
        //根据设备ID集合查找出磅房的监控设备列表
        List<FvsDeviceDTO> fvsDeviceList = fvsDeviceService.list(new ArrayList<>(map.keySet()));
        for (FvsDeviceDTO fvsDeviceDTO : fvsDeviceList) {
            FmamWeighRoomFvsDeviceDTO weighRoomFvsDevice = map.get(fvsDeviceDTO.getId());
            if (weighRoomFvsDevice != null) {
                weighRoomFvsDevice.setFvsDeviceName(fvsDeviceDTO.getName());
                weighRoomFvsDevice.setDeviceStatus(fvsDeviceDTO.getDeviceStatus());
                weighRoomFvsDevice.setDeviceType(fvsDeviceDTO.getDeviceType());
                weighRoomFvsDevice.setSnapshotUrl(fvsDeviceDTO.getSnapshotUrl());
                weighRoomFvsDevice.setPlayMode(fvsDeviceDTO.getPlayMode());
                weighRoomFvsDevice.setPtz(fvsDeviceDTO.getPtz());
                weighRoomFvsDevice.setStreamStatus(fvsDeviceDTO.getStreamStatus());
            }
        }
        return new ArrayList<>(map.values());
    }

    /**
     * 获取磅房未添加的项目视频监控列表
     *
     * @param deptId
     * @param weighRoomId
     * @return
     */
    @Override
    public List<FmamWeighRoomFvsDeviceDTO> projectVideoList(Integer deptId, Integer weighRoomId) {

        Map<Integer, FmamWeighRoomFvsDeviceDTO> map = this.projectVedioList(deptId);
        if (!map.isEmpty()) {
            //查找磅房已经添加的视频列表
            List<FmamWeighRoomFvsDeviceDTO> weighRoomFvsDevics = fmamWeighRoomService.fvsDeviceList(weighRoomId);
            if (weighRoomFvsDevics != null && weighRoomFvsDevics.size() > 0) {
                for (FmamWeighRoomFvsDeviceDTO weighRoomFvsDevice : weighRoomFvsDevics) {
                    Integer fvsDeviceId = weighRoomFvsDevice.getFvsDeviceId();
                    //项目上已添加的视频设备从map中移除
                    if (map.containsKey(fvsDeviceId)) {
                        map.remove(fvsDeviceId);
                    }
                }
            }
        }
        return new ArrayList<>(map.values());
    }

    /**
     * 查询项目所有视频列表
     */
    private Map<Integer, FmamWeighRoomFvsDeviceDTO> projectVedioList(Integer deptId) {
        Map<Integer, FmamWeighRoomFvsDeviceDTO> map = new HashMap<>();

        FvsDeviceListParam fvsDeviceListParam = new FvsDeviceListParam();
        fvsDeviceListParam.setDeptId(deptId);
        fvsDeviceListParam.setPageNum(1);
        fvsDeviceListParam.setPageSize(10000);
        PageData<FvsDeviceDTO> fvsDeviceDTOPageData = fvsDeviceService.deviceList(fvsDeviceListParam);
        List<FvsDeviceDTO> list = fvsDeviceDTOPageData.getList();
        if (list != null && list.size() > 0) {
            //取出设备ID 查找监控设备信息
            for (FvsDeviceDTO fvsDeviceDTO : list) {
                if (fvsDeviceDTO.getDeviceType().equals(FvsDeviceType.IPC.getValue())) {
                    //网络摄像头
                    FmamWeighRoomFvsDeviceDTO weighRoomFvsDeviceDTO = new FmamWeighRoomFvsDeviceDTO();
                    weighRoomFvsDeviceDTO.setFvsDeviceId(fvsDeviceDTO.getId());
                    weighRoomFvsDeviceDTO.setFvsDeviceName(fvsDeviceDTO.getName());
                    weighRoomFvsDeviceDTO.setDeviceType(fvsDeviceDTO.getDeviceType());
                    map.put(fvsDeviceDTO.getId(), weighRoomFvsDeviceDTO);
                }
                if (fvsDeviceDTO.getDeviceType().equals(FvsDeviceType.DVR.getValue())) {
                    //硬盘录像机,
                    List<FvsDeviceDTO> childrenList = fvsDeviceDTO.getChildren();
                    if (childrenList != null && childrenList.size() > 0) {
                        //取出监控工设备放入map
                        for (FvsDeviceDTO deviceDTO : childrenList) {
                            FmamWeighRoomFvsDeviceDTO weighRoomFvsDeviceDTO = new FmamWeighRoomFvsDeviceDTO();
                            weighRoomFvsDeviceDTO.setFvsDeviceId(deviceDTO.getId());
                            weighRoomFvsDeviceDTO.setFvsDeviceName(deviceDTO.getName());
                            weighRoomFvsDeviceDTO.setDeviceType(deviceDTO.getDeviceType());
                            map.put(deviceDTO.getId(), weighRoomFvsDeviceDTO);
                        }

                    }
                }
            }
        }
        return map;
    }


    /**
     * 磅房添加视频监控设备
     *
     * @param fvsDeviceAddQO
     */
    @Override
    public void addVideo(WeighRoomFvsDeviceAddQO fvsDeviceAddQO) {
        fmamWeighRoomService.addWeighRoomFvsDevice(fvsDeviceAddQO);
    }

    /**
     * 磅房视频监控直播地址
     *
     * @param deviceId
     * @return
     */
    @Override
    public FvsStreamDTO videoStreamURL(Integer deviceId) {
        return fvsDeviceService.getStreamUrl(deviceId);
    }

    /**
     * 磅房视频监控回放
     *
     * @param deviceId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public ListData<FvsRecordDTO> videoRecordList(Integer deviceId, Date startTime, Date endTime) {
        return fvsDeviceService.getRecordList(deviceId, startTime, endTime);
    }


    /**
     * 磅房视频监控设备删除
     *
     * @param weighRoomId
     * @param deviceId
     */
    @Override
    public void videoDel(Integer weighRoomId, Integer deviceId) {
        fmamWeighRoomService.videoDel(weighRoomId, deviceId);
    }


}
